package com.jp.kefu.service;

import com.jp.kefu.web.handler.MessageHandler;
import com.jp.kefu.util.XMLUtils;
import com.jp.kefu.web.message.AbstractMessage;
import com.jp.kefu.web.message.common.CommonMessage;
import com.jp.kefu.web.handler.MessageHandlerRegistry;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * Created by Administrator on 2017/7/25.
 */
@Service
public class MessageService {
    public String receive(String reqMsg){
        Assert.notNull(reqMsg, "reqMsg is null");
        AbstractMessage msg = XMLUtils.parse(reqMsg);


        MessageHandler messageHandler = MessageHandlerRegistry.get(msg.getClass());
        messageHandler.handle(msg);
        //响应消息
        CommonMessage reply = reply(msg);
        if(reply == null){
            return "";
        }else{
            return XMLUtils.print(msg);
        }
    }

    //自定义自动回复内容，默认回复空串
    private CommonMessage reply(AbstractMessage reqMsg){
        return null;
    }
}
